﻿package com.d4p.util
{
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.Event;

	public class MouseMove extends MovieClip
	{
		protected var targetFrame:FrameLabel;
		public function MouseMove()
		{
			super();
		}
		protected function eventHandler_onEnterFrame(e:Event):void{
			if(this.currentFrame == targetFrame.frame){
				stopFrame();
			}else{
				(this.currentFrame > targetFrame.frame) ? prevFrame() : nextFrame();
			}
		}


		public function playFrame(labelName:String):void{
			stopFrame();
			setTargetFrame(labelName);
			this.addEventListener(Event.ENTER_FRAME, eventHandler_onEnterFrame);
		}

		public function stopFrame():void{
			if(!hasEventListener(Event.ENTER_FRAME)) return;
			removeEventListener(Event.ENTER_FRAME, eventHandler_onEnterFrame);
		}
		
		private function setTargetFrame(labelName:String):void{
			var labels:Array = this.currentLabels;
			
			var isTargetFrame:Function = function(element:*, index:int, arr:Array):Boolean{
				var result:Boolean = false;
				if(element.name == labelName){
					targetFrame = element;
					result = true;
				}
				return result;
			}
			
			labels.filter(isTargetFrame);
		}		
		
		private function getTargetFrame():FrameLabel{
			return targetFrame;
		}
	}
}